package ai.people.netmon.framework.domain.graph;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * 前端图格式 v2
 *
 * @author zhenglin
 * @date 2022/4/24
 */
@Data
@NoArgsConstructor
public class FrontGraphBaseV2 {
    /**
     * 节点
     */
    @Size(min = 1, message = "节点数量不能为0")
    @Valid
    Set<Node> nodes;
    /**
     * 边
     */
    @Valid
    Set<Edge> edges;

    @Data
    public static class Node {
        /**
         * id
         */
        @NotBlank(message = "节点id不能为空")
        private String id;

        /**
         * 节点类型  1 主体，2：自动，3：手动，4：扩展，5：碰撞
         */
        @NotBlank(message = "节点类型不能为空")
        private String type;

        /**
         * 父节点 id
         */
        private String pid;

        /**
         * 节点业务类型 TW，FB等业务节点类型
         */
        @NotBlank(message = "节点业务类型不能为空")
        private String nodesType;

        /**
         * 是否是主节点
         */
        private boolean main;
        /**
         * 坐标轴x
         */
        private Double x;

        /**
         * 坐标轴y
         */
        private Double y;

        /**
         * 属性
         */
        private Map<String, Object> properties;


        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Node)) return false;
            Node node = (Node) o;
            return main == node.main && Objects.equals(type, node.type) && Objects.equals(nodesType, node.nodesType) && Objects.equals(properties, node.properties);
        }

        @Override
        public int hashCode() {
            return Objects.hash(type, nodesType, properties);
        }
    }

    @Data
    public static class Edge {
        /**
         * 出边node id
         */
        @NotBlank(message = "source id不能为空")
        private String source;
        /**
         * 入边node id
         */
        @NotBlank(message = "target id不能为空")
        private String target;
        /**
         * 开始箭头
         */
        private Boolean startArrow;
        /**
         * 结束箭头
         */
        private Boolean endArrow;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Edge)) return false;
            Edge edge = (Edge) o;
            return (Objects.equals(source, edge.source) || Objects.equals(source, edge.target)) &&
                    (Objects.equals(target, edge.target) || Objects.equals(target, edge.source)) ;
        }

        @Override
        public int hashCode() {
            return Objects.hash(source, target) + Objects.hash(target, source);
        }
    }
}
